package sybel.pan;

import java.awt.Dialog.ModalityType;
import java.awt.Window;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 *
 * @author Stefano
 */
public class newEventExt extends javax.swing.JPanel {

    /** Creates new form newEventExt */
    public newEventExt(Node flow) {
        initComponents();
        Flow = flow;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jButCancel = new javax.swing.JButton();
        jButDone = new javax.swing.JButton();

        jLabel1.setText("Choose event or extension to create:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Interaction", "Happening", "Alternative", "Include" }));

        jButCancel.setText("Cancel");
        jButCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButCancelActionPerformed(evt);
            }
        });

        jButDone.setText("Done");
        jButDone.setMaximumSize(new java.awt.Dimension(65, 23));
        jButDone.setMinimumSize(new java.awt.Dimension(65, 23));
        jButDone.setPreferredSize(new java.awt.Dimension(65, 23));
        jButDone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButDoneActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButDone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButCancel)
                    .addComponent(jButDone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButCancelActionPerformed
        dialog.dispose();
    }//GEN-LAST:event_jButCancelActionPerformed

    private void jButDoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButDoneActionPerformed
        Flow = newFlowChild(jComboBox1.getSelectedIndex());
        if (Flow != null){
            dialog.dispose();
        }
    }//GEN-LAST:event_jButDoneActionPerformed

    public Node show(Window owner){
        dialog = new JDialog(owner,"New Behaviour",ModalityType.APPLICATION_MODAL);
        dialog.setContentPane(this);
        dialog.pack();
        dialog.setVisible(true);
        return Flow;
    }

    /**
     * Metodo che crea un elemento del flow
     *
     * @param event intero che rappresenta il nodo da aggiungere
     * @param flow a cui và aggiunto il nodo
     * @return il Nodo appena aggiunto al flow
     */
    public Node newFlowChild(int event){
        Document doc = Flow.getOwnerDocument();
        Element flowchild = null;
        try{
            switch(event){
            // new Interaction
            case 0:
                flowchild = doc.createElement("interaction");
                break;
            // new Happening
            case 1:
                flowchild = doc.createElement("happening");
                break;
            // new Alternative
            case 2:
                flowchild = doc.createElement("alternative");
                break;
            // new Include
            case 3:
                flowchild = doc.createElement("include");
                break;
            }

            // appendo l'elemento creato come ultimo figlio del "flow"

            if(Flow.hasChildNodes() && (Flow.getLastChild().getNodeName() == "success" || Flow.getLastChild().getNodeName() == "failure"
                    || Flow.getLastChild().getNodeName() == "goto"))
                Flow.insertBefore(flowchild,Flow.getLastChild());
            else
                Flow.appendChild(flowchild);
        }catch (DOMException dex){
            JOptionPane.showMessageDialog(null, "ERRORE: "+dex.getMessage(),"An error has occured" ,1);
            return null;
        }
        return (Node)flowchild;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButCancel;
    private javax.swing.JButton jButDone;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
    private javax.swing.JDialog dialog;
    private Node Flow = null;

}
